package org.interactive.spring.intro.controller.mvc;

import org.interactive.spring.intro.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * UsersMVCController class
 *
 * @author bogdan.solga
 *
 * Date: 12.06.2013, time: 14:33
 */
@Controller
@RequestMapping(value = "/mvc", method = RequestMethod.GET)
public class UsersMVCController {

    @Autowired
    private UsersService usersService;

    @RequestMapping(value = "/users")
    public ModelAndView modelAndViewExample() {
        ModelAndView modelAndView = new ModelAndView("users");

        modelAndView.addObject("users", usersService.getAllUsers(null));

        return modelAndView;
    }

    @RequestMapping(value = "/users/filtered")
    public ModelAndView modelAndViewEnhanced(@RequestParam(required = false) String userName) {
        ModelAndView modelAndView = new ModelAndView("users");

        modelAndView.addObject("users", usersService.getAllUsers(userName));
        modelAndView.addObject("filtering", userName != null);

        return modelAndView;
    }

    /*
    @RequestMapping(value = "/users/model")
    public String justModel(ModelMap model) {
        model.addAttribute("users", usersService.getAllUsers(null));
        model.addAttribute("filtering", false);

        return "users";
    }
    */
}
